import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Switch, InputNumber } from 'ant-design-vue';
import { getOptions, getRoleUnit, getChangeGroupOrder } from '/@/api/system/system';
import { useMessage } from '/@/hooks/web/useMessage';
import { useUserStore } from '/@/store/modules/user';
import { useI18n } from '/@/hooks/web/useI18n';

const userStore = useUserStore();
const userinfo = userStore.getUserInfo;
const { createMessage } = useMessage();
const { t } = useI18n();

export const records = {};

//1：true 0:false
export const columns: BasicColumn[] = [
  {
    title: '序号',
    dataIndex: 'orders',
    width: 30,
    ellipsis: true,
    customRender: ({ record }: any) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(InputNumber, {
        value: record.orders,
        loading: record.pendingStatus,
        style: { width: '60px' },
        onChange: (e) => {
          record.orders = e;
        },
        onBlur: () => {
          if (record.orders == record.oldOrders) {
            return;
          }
          record.pendingStatus = true;
          getChangeGroupOrder({ code: record.code, order: record.orders })
            .then(() => {
              createMessage.success(t('common.opSuccess'));
            })
            .catch(() => {
              record.orders = record.oldOrders;
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '类别',
    dataIndex: 'kindName',
    width: 100,
    ellipsis: true,
  },
  {
    title: '名称',
    dataIndex: 'desc',
    width: 150,
    ellipsis: true,
  },
  {
    title: '单位',
    dataIndex: 'unitName',
    width: 100,
    ellipsis: true,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'searchUnitCode',
    label: '单位',
    component: 'ApiSelect',
    colProps: { span: 6 },
    componentProps: {
      api: getRoleUnit,
      params: {},
      resultField: 'list',
    },
  },
  {
    field: 'kind',
    label: '类别',
    component: 'ApiSelect',
    componentProps: {
      api: getOptions,
      labelField: 'name',
      valueField: 'value',
      params: {
        code: 'usergroup_kind',
      },
    },
    colProps: { span: 6 },
  },
  {
    field: 'what',
    label: '名称',
    component: 'Input',
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  // {
  //   field: 'code',
  //   label: '编码',
  //   required: true,
  //   component: 'Input',
  // },
  {
    field: 'desc',
    label: '名称',
    required: true,
    component: 'Input',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'kind',
    label: '类别',
    component: 'ApiSelect',
    required: false,
    colProps: {
      span: 24,
    },
    defaultValue: '1',
    componentProps: {
      api: getOptions,
      labelField: 'name',
      valueField: 'value',
      params: {
        code: 'usergroup_kind',
      },
    },
  },
  {
    field: 'orders',
    label: '序号',
    required: true,
    defaultValue: 1,
    component: 'Input',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'unitCode',
    label: '单位',
    component: 'ApiSelect',
    defaultValue: userinfo.unitCode ? userinfo.unitCode : 'root',
    colProps: {
      span: 24,
    },
    componentProps: {
      api: getRoleUnit,
      params: {},
      resultField: 'list',
    },
  },
];
